(function(){

var wid=$('#svgbox').width(),
	hei=$('#svgbox').height(),
	self=this;
	
var shuxianone=wid/420*135,
	shuxiantwo=wid/420*225,
	danxian=wid/420,
	henxian=hei*0.7/6,
	orq=hei*0.7,
	orl=hei*0.3,
	orlline=hei*0.3/2,
	linewid=wid/420/2;
var data=[];
var lenghtss;

$.ajax({
	type:"get",
	url:urls,
	dataType:'json',
	async:true,
	success:function(datasd){
		data=datasd;
		lenghtss=datasd['tu'].length;
		init();
	}
});


setInterval(function(){
	$.ajax({
		type:"get",
		url:urls,
		dataType:'json',
		async:true,
		success:function(datasd){
			data=datasd;
			lenghtss=datasd['tu'].length;
			init();
		}
	});
	
},60000)
function makeSVG(tag, attrs) {
    var el= document.createElementNS('http://www.w3.org/2000/svg', tag);
    for (var k in attrs)
        el.setAttribute(k, attrs[k]);
    return el;
}
function init(){
	dirdata();
	redline();
	picdark();
	linedark();
	circle();
}
var dsvg=$('#svgbox').find('g').eq(0);
function bakc(){
	var shulineone=makeSVG('line', {x1: wid*0.5046, x2: wid*0.5046, y1:0, y2:orq, stroke: '#f8f8f8'});
		dsvg.append(shulineone);
	var shulinetwo=makeSVG('line', {x1: wid*0.72031, x2: wid*0.72031, y1:0, y2:orq, stroke: '#f8f8f8'});
	dsvg.append(shulinetwo);
	for(var i=0;i<6;i++){
		var henline=makeSVG('line', {x1: 0, x2: wid, y1:henxian*(i+1), y2:henxian*(i+1), stroke: '#f8f8f8'});
		dsvg.append(henline);
	}
	dsvg.find('text').eq(0).attr('y',hei*0.7+14);
	dsvg.find('text').eq(1).attr('x',wid*0.5046);
	dsvg.find('text').eq(1).attr('y',hei*0.7+14);
	dsvg.find('text').eq(2).attr('x',(wid*0.72031));
	dsvg.find('text').eq(2).attr('y',hei*0.7+14);
	dsvg.find('text').eq(3).attr('x',(wid-35));
	dsvg.find('text').eq(3).attr('y',hei*0.7+14);
	var orlone=makeSVG('line', {x1: 0, x2: wid, y1:hei*0.7+orlline*1, y2:hei*0.7+orlline*1, stroke: '#f8f8f8'});
		dsvg.append(orlone);
	var orltwo=makeSVG('line', {x1: 0, x2: wid, y1:hei*0.7+orlline*2-1, y2:hei*0.7+orlline*2-1, stroke: '#f8f8f8'});
		dsvg.append(orltwo);
};

var tu,fenxian,baifen,jiange;
function dirdata(){
	tu=data['tu'];
	fenxian=data['shi'];
	jiange=fenxian.hign-fenxian.low;
	baifen=hei*0.7/jiange;
	dsvg.find('text').eq(4).find('tspan').html(fenxian.open);
	
	dsvg.find('text').eq(5).find('tspan').html(fenxian.hign);
	dsvg.find('text').eq(5).find('tspan').attr('y',hei*0.7-hei*0.7/jiange*(fenxian.hign-fenxian.low)+20);
	
	dsvg.find('text').eq(6).find('tspan').html(fenxian.low);
	dsvg.find('text').eq(6).find('tspan').attr('y',hei*0.7-hei*0.7/jiange*1-10);
	
	console.log(tu[0].ys+'/'+fenxian.low);
}
function redline(){
	var stappp=hei*0.7/jiange*(fenxian.open-fenxian.low);
	dsvg.find('line').eq(0).attr('x1',0);
	dsvg.find('line').eq(0).attr('x2',wid);
	dsvg.find('line').eq(0).attr('y1',stappp);
	dsvg.find('line').eq(0).attr('y2',stappp);
	dsvg.find('text').eq(4).attr('y',stappp+14);
}
var osvg=$('#svgbox').find('g').eq(1),
	osvgline=osvg.find('path').eq(1),
	osvgpic=osvg.find('path').eq(0),
	last;
function picdark(){	
	var start=hei*0.7/jiange*(tu[0].ys-fenxian.low),
		lineone="M 0,"+(hei*0.7-start);
	for(var i=1;i<lenghtss;i++){
		if(i>0){
			var bod=(tu[i].ys-fenxian.low)*hei*0.7/jiange;
			lineone=lineone+"L"+(i+1)*wid/780+","+(hei*0.7-bod);
		}
		
	}
		lineone=lineone+"L"+lenghtss*wid/780+","+hei*0.7 +"L"+0+","+hei*0.7 +"Z";
		
	var linetwo="M 0,"+(hei*0.7-start);
	for(var i=0;i<lenghtss;i++){
		var bod=(tu[i].ys-fenxian.low)*hei*0.7/jiange;
			last=bod;
		linetwo=linetwo+"L"+(i+1)*wid/780+","+(hei*0.7-bod);
	}
	osvgline.attr('d',linetwo);
	osvgpic.attr('d',lineone);
};
var linesvg=$('#svgbox').find('g').eq(2);
function linedark(){
	linesvg.find('line').remove();
	var rels='';
	for(var i=0;i<lenghtss;i++){
		var henline=makeSVG('line', {x1:i*wid/780, x2: i*wid/780, y1:hei, y2:hei-(hei*0.3/10000*tu[i].zs), stroke: '#d0402d','stroke-width':linewid});
		
		linesvg.append(henline);
	}	
};

function circle(){
	osvg.find('circle').remove();
	var circle= makeSVG('circle', {cx: (lenghtss)*wid/780, cy: (hei*0.7-last), r:2, stroke: 'none', 'stroke-width': "none", fill: 'red'});
	osvg.append(circle);
}

})()//闭包
